const app = getApp();
const regeneratorRuntime = global.regeneratorRuntime = require("../libs/regenerator");
const co = require('../libs/co');
const passport = require("../services/passport/passport");

Page({
    data: {
      carts:[],
      totalPrice: 0,
      ship: 0,
      selectAllStatus: false
    },
    onLoad: function(options) {
        app.launchOptions = options;
        /* !Do not Remove the follow line, used for build tool.*/
        /*{ComponentLaunchJs}*/
    },
    onShow: function() {
      this.loadProducts();
    },
    loadProducts: function () {
      var that = this;
      co(function *() {
          let cartService = require("../services/mall/cartService");
          let products = yield cartService.getCartList(0,10);
          that.data.carts = [];
          for (var i in products) {
            let product = products[i];
            product.selected = false;
            that.data.carts.push(product);
          }
          that.setData({
              carts: that.data.carts
          });
          that.getTotalPrice();
          that.getSelectAllStatus();
      });
    },
    /**
    * 选中当前商品
    */
    selectList(e) {
        const index = e.currentTarget.dataset.index;
        let carts = this.data.carts;
        const selected = carts[index].selected;
        carts[index].selected = !selected;

        this.setData({
          carts: carts
        });
        this.getTotalPrice();
        this.getSelectAllStatus();
    },
    /**
   * 全选
   */
  selectAll(e) {
    let selectAllStatus = this.data.selectAllStatus;
    selectAllStatus = !selectAllStatus;
    let carts = this.data.carts;

    for (let i = 0; i < carts.length; i++) {
      carts[i].selected = selectAllStatus;
    }
    this.setData({
      selectAllStatus: selectAllStatus,
      carts: carts
    });
    this.getTotalPrice();
  },
   /**
   * 计算总价
   */
    getTotalPrice() {
      let carts = this.data.carts;                  // 获取购物车列表
      let total = 0;
      for(let i = 0; i< carts.length; i++) {         // 循环列表得到每个数据
        if(carts[i].selected) {                     // 判断选中才会计算价格
          total += carts[i].selectedNumber * carts[i].selectedAmount;   // 所有价格加起来
        }
      }
      this.setData({                                // 最后赋值到data中渲染到页面
        carts: carts,
        totalPrice: total.toFixed(2)
      });
    },
    /**
    * 获取全选状态
    */
    getSelectAllStatus () {
      let carts = this.data.carts;
      let selectStatus = false;
      for (var i = 0; i < carts.length; i++) {
         selectStatus = carts[i].selected;
         if (!selectStatus) {
           break;
         }
      }
      this.setData({
        selectAllStatus:selectStatus
      });
    },
    /**
   * 删除购物车当前商品
   */
  deleteList(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    let that = this;
    co(function *() {
      let productId = carts[index].productId;
      let skuId = carts[index].skuId;
      let cartService = require("../services/mall/cartService");
      let flag = yield cartService.deleteCart(productId,skuId);
      if (flag) {
        carts.splice(index,1);
        that.setData({
          carts: carts
        });
        if(!carts.length){
          that.setData({
            hasList: false
          });
        }else{
          that.getTotalPrice();
        }
      } else {
        wx.showToast({
         title: '删除失败',
         icon: 'success',
         duration: 1000
       });
      }
    });
  },
    /**
       * 绑定加数量事件
       */
    addCount:function(e) {
        const index = e.currentTarget.dataset.index;
        let carts = this.data.carts;
        let num = carts[index].selectedNumber;
        num = num + 1;
        carts[index].selectedNumber = num;
        this.updateNum(carts[index].productId,2,carts[index].skuId);
        this.setData({
          carts: carts
        });
        this.getTotalPrice();
    },
    /**
       * 绑定减数量事件
       */
      minusCount:function(e) {
       const index = e.currentTarget.dataset.index;
       let carts = this.data.carts;
       let num = carts[index].selectedNumber;
       if(num <= 1){
         return false;
       }
       num = num - 1;
       carts[index].selectedNumber = num;
       this.updateNum(carts[index].productId,1,carts[index].skuId);
       this.setData({
         carts: carts
       });
       this.getTotalPrice();
     },
     updateNum: function (productId,type,skuId){
       var that = this;
       co(function *() {
         let cartService = require("../services/mall/cartService");
         let flag = yield cartService.updateCount(productId,type,skuId);
       });
     },
     goBuy: function() {
       let carts = this.data.carts;
       let skuIds = "";
       for (var i in carts) {
         let product = carts[i];
         if (product.selected) {
           skuIds += product.skuId + ",";
         }
       }
       skuIds = skuIds == "" ? skuIds : skuIds.substr(0,skuIds.length - 1);
       if (skuIds == "") {
         wx.showModal({
           title: '提示',
           content: '请选择商品',
           text:'center',
           showCancel: false
         })
       } else {
         wx.navigateTo({
             url: '/pages/page10?skuId=' + skuIds,
         });
       }
     },
    onReady:function(){
    },
    /* !Do not Remove the follow line, used for build tool.*/
    /*{ComponentJs}*/

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }

});
